package com.hongye.house.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hongye.common.entity.UserHomeEntity;
import com.hongye.common.utils.PageUtils;
import com.hongye.common.utils.R;
import com.hongye.house.entity.RequirementEntity;
import com.hongye.house.fegin.ProprietaryFeign;
import com.hongye.house.service.RequirementService;
import com.hongye.house.vo.RentSeekingVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("house/requirement")
public class RequirementController {
    @Autowired
    private RequirementService requirementService;
    @Autowired
    private ProprietaryFeign proprietaryFeign;

    /**
     * 求租，求购列表列表
     */
    @RequestMapping("/RentHouseList")
    public R RentHouseList(@RequestParam(value = "yid",defaultValue = "") Integer yid,
                           @RequestParam(value = "hometype",defaultValue = "") Integer hometype,
                           @RequestParam(value = "type",defaultValue = "") Integer type){
        List<RequirementEntity> requirement_yid=null;
        ArrayList<RentSeekingVo> rentSeekingVos = new ArrayList<>();
        if (yid!=null && hometype!=null){
            requirement_yid = requirementService.list(new QueryWrapper<RequirementEntity>()
                            .eq("requirement_yid", yid)
                            .eq("requirement_hometype",hometype)
                            .eq("requirement_type",type)
            );
        }
        if (yid!=null){
         requirement_yid = requirementService.list(new QueryWrapper<RequirementEntity>().eq("requirement_yid", yid).eq("requirement_type",type));
        }
        if (hometype!=null){
            requirement_yid = requirementService.list(new QueryWrapper<RequirementEntity>().eq("requirement_type", hometype).eq("requirement_type",type));
        }
        if (yid==null && hometype==null){
            requirement_yid = requirementService.list(new QueryWrapper<RequirementEntity>().eq("requirement_type",type));

        }
        List<UserHomeEntity> getlist = proprietaryFeign.getlist();
        for (RequirementEntity requirementEntity : requirement_yid) {
            for (UserHomeEntity userHomeEntity : getlist) {
                if (requirementEntity.getRequirementUserid()==userHomeEntity.getUserId()){
                    RentSeekingVo rentSeekingVo = new RentSeekingVo();
                    BeanUtils.copyProperties(requirementEntity,rentSeekingVo);
                    BeanUtils.copyProperties(userHomeEntity,rentSeekingVo);
                    rentSeekingVos.add(rentSeekingVo);
                }

            }

        }


        return R.ok().put("data", rentSeekingVos);
    }



    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = requirementService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{requirementId}")
    public R info(@PathVariable("requirementId") Integer requirementId){
		RequirementEntity requirement = requirementService.getById(requirementId);

        return R.ok().put("requirement", requirement);
    }

    /**
     * 求租求购发布接口
     */
    @RequestMapping("/save")
    public R save(@RequestBody RequirementEntity requirement){
        requirementService.save(requirement);

        return R.ok().put("data", JSON.toJSONString(requirement));
    }


    /*
    求租的信息传入es
     */
    @Scheduled(cron = "0 30 12 * * *")
    public void esrentinginsert(){
        requirementService.esrentinginsert();
    }

    /*
   求购的信息传入es
    */
    @Scheduled(cron = "0 30 12 * * *")
    public void esbuyhouseinsert(){
        requirementService.esbuyhouseinsert();
    }




    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody RequirementEntity requirement){
		requirementService.updateById(requirement);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] requirementIds){
		requirementService.removeByIds(Arrays.asList(requirementIds));

        return R.ok();
    }

}
